<template>
  <!-- 搜了下这文件貌似没用到 -->
  <div class="common-emoji">
    <div class="emoji-main">
      <el-popover
        ref="popover"
        v-model="isEmojiShown"
        popper-class="emoji-popover"
        placement="top-start"
        width="400"
        trigger="click">
        <i
          slot="reference"
          style="color:#8C8C8C;"
          class="iconfont icon-smile">
        </i>
        <div class="m-chat-emoji" v-if="emojiInit">
          <div class="emoji-content">
            <div class="cnt">
              <span
                class="emoji-item"
                v-for="(name, index) in emojiList"
                :title="name"
                @click.stop="addEmoji(`[${name}]`)"
                :key="index">
                <img :src="`${emojiMap.get(`[${name}]`).url}`" style="width:32px;">
              </span>
            </div>
          </div>
        </div>
      </el-popover>
    </div>
  </div>
</template>

<script>
import emojiConfig from './emoji'

export default {
  name: 'common-emoji',
  props: {
    inputRef: {

    }
  },
  data () {
    return {
      emojiList: emojiConfig.emojiList,
      emojiMap: emojiConfig.emojiMap,
      emojiSow: false,

      position: void 0,
      emojiInit: false,
      isEmojiShown: false
    }
  },
  computed: {
  },
  watch: {
    isEmojiShown(newValue) {
      if (newValue) {
        this.showEmoji()
      } else {
        this.hideEmoji()
      }
    }
  },
  methods: {
    // 失焦时暂时保存光标位置
    contentBlur (el) {
      this.position = this.getCursortPosition(el)
      setTimeout(() => {
        this.position = void 0
      }, 500)
    },
    // toggleEmojiBox () {
    //   this.isEmojiShown = !this.isEmojiShown
    //   if (this.isEmojiShown) {
    //     this.showEmoji()
    //   } else {
    //     this.hideEmoji()
    //   }
    // },
    showEmoji () {
      this.emojiInit = true
      this.emojiShow()
      this.$nextTick(() => {
        this.isEmojiShown = true
      })
    },
    emojiShow () {
      this.focusAndAdd()
    },
    hideEmoji () {
      this.isEmojiShown = false
      // this.emojiHide()
    },
    emojiHide () {
      this.isHideEmoji = false
    },
    addEmoji (emojiName) {
      this.inputMsg(emojiName)
      this.hideEmoji()
    },
    // 添加标签文本
    focusAndAdd (txt) {
      let inputRef = this.inputRef || this.$parent.$refs.replyInput
      let el = inputRef.$el.querySelector('textarea')
      this.position || (this.position = {
        start: el.value.length,
        end: el.value.length
      })
      this.setCaretPosition(el, this.position)
      txt && this.insertAtCursor(el, txt)
    },
    // 获取光标位置函数
    getCursortPosition (ctrl) {
      let CaretPos = void 0
      if (document.selection) { // For IE
        ctrl.focus()
        let Sel = document.selection.createRange()
        Sel.moveStart('character', -ctrl.value.length)
        CaretPos = Sel.text.length
      } else if (ctrl.selectionStart || ctrl.selectionStart === 0) {
        CaretPos = {
          start: ctrl.selectionStart,
          end: ctrl.selectionEnd
        }
      }
      return CaretPos
    },
    // 设置光标位置函数
    setCaretPosition (ctrl, pos) {
      if (ctrl.setSelectionRange) {
        ctrl.focus()
        ctrl.setSelectionRange(pos.start, pos.end)
      } else if (ctrl.createTextRange) { // For IE
        let range = ctrl.createTextRange()
        range.collapse(true)
        range.moveEnd('character', pos.start)
        range.moveStart('character', pos.end)
        range.select()
      }
    },
    // 光标位置插入文本，isRange：插入后是否是选中状态
    insertAtCursor (myField, myValue, isRange = false) {
      if (document.selection) { // For IE
        myField.focus()
        let sel = document.selection.createRange()
        sel.text = myValue
      } else if (myField.selectionStart || myField.selectionStart === 0) {
        let startPos = myField.selectionStart
        let endPos = myField.selectionEnd
        let value = myField.value.substring(0, startPos) +
            myValue +
            myField.value.substring(endPos, myField.value.length)
        this.$emit('changeInput', value)
        this.$nextTick(() => {
          myField.selectionStart = startPos + (isRange ? 0 : myField.value.length)
          myField.selectionEnd = startPos + myValue.length
        })
      } else {
        let value = this.formData.content + myValue
        this.$emit('changeInput', value)
      }
    },
    onInputFocus (e) {
      setTimeout(() => {
        // todo fixme 解决iOS输入框被遮挡问题，但会存在空白缝隙
        e.target.scrollIntoView()
        // pageUtil.scrollChatListDown()
      }, 200)
    },
    // 获取带有表情的信息
    inputMsg (msg) {
      this.focusAndAdd(msg)
    },
    selectEmoji (emoji) {
      // 由触发父组件事件，增加表情文案
      this.$emit('add-emoji', emoji)
    }
  }
}
</script>

<style lang="scss" scoped>
  .common-emoji {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: 12px;
    height: 16px;
    line-height: 16px;
    .emoji-main {
      position: relative;
      display: initial;
      .iconfont {
        color: #8c8c8c;
      }
    }
  }
  .popover {
    height: 200px;
  }

  .m-chat-emoji {
    position: absolute;
    top: 0;
    left: 0;
    width: 400px;
    background-color: #fff;
    .emoji-content {
      position: relative;
      width: 100%;
      height: 200px;
      background: #fff;
      overflow-y: auto;
      .cnt {
        position: relative;
        display: block;
        margin: 8px auto;
        text-align: left;
      }
      .emoji-item {
        display: inline-block;
        width: 28px;
        height: 28px;
        padding: 2px;
        vertical-align: middle;
        /*border: 1px solid #fff;*/
        /*margin-left: -1px;*/
        /*margin-bottom: -1px;*/
        img {
          width:inherit ;
          height: inherit;
        }
      }
      .pinup-item {
        width: 28px;
        height: 28px;
      }
    }

    .emoji-content .cnt {
      width: 100%;
    }
  }

</style>

<style lang="scss">
  .emoji-popover.el-popover {
    height: 200px;
    border-radius: 8px;
    overflow: hidden;
  }
</style>